﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = false)]
namespace CKS2320.Controller
{
    /// <summary>
    /// 日志等级
    /// </summary>
    public enum LogLevel
    {
        Error,
        Debug,
        Warning,
        Info
    }
    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogType
    {
        InfoLog,
        SysLog,
        OP10Log,
        OP20Log,
        OP30Log,
        KeyenceLog,
    }
    /// <summary>
    /// 日志记录服务
    /// </summary>
    public class C_LogWriter
    {
        private ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim();
        /// <summary>
        /// 日志接口
        /// </summary>
        private log4net.ILog m_Log;
        public C_LogWriter(LogType? _LogType)
        {
            Init(_LogType);
        }

        public void Init(LogType? _LogType)
        {
            if (_LogType != null)
            {
                string? s = _LogType?.ToString();
                m_Log = log4net.LogManager.GetLogger(s);
            }
        }
        /// <summary>
        /// 输出错误级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Error(string message, LogType? _LogType = null)
        {
            if (_LogType != null)
                Init(_LogType);

            //记录日志
            WriteLog(LogLevel.Error, message);
        }

        /// <summary>
        /// 输出警告级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Warning(string message, LogType? _LogType = null)
        {
            if (_LogType != null)
                Init(_LogType);
            //记录日志
            WriteLog(LogLevel.Warning, message);
        }

        /// <summary>
        /// 输出信息级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Info(string message, LogType? _LogType = null)
        {
            if (_LogType != null)
                Init(_LogType);
            //记录日志
            WriteLog(LogLevel.Info, message);
        }

        /// <summary>
        /// 输出调试级别日志
        /// </summary>
        /// <param name="message">输出的消息</param>
        public void Debug(string message, LogType? _LogType = null)
        {
            if (_LogType != null)
                Init(_LogType);
            //记录日志
            WriteLog(LogLevel.Debug, message);
        }

        /// <summary>
        /// 记录系统日志
        /// </summary>
        /// <param name="logLevel">日志级别</param>
        /// <param name="message">输出的消息</param>
        private void WriteLog(LogLevel logLevel, string message)
        {
            readerWriterLockSlim.EnterWriteLock();
            try
            {
                switch (logLevel)
                {
                    case LogLevel.Debug:
                        m_Log.Debug(message);
                        break;
                    case LogLevel.Error:
                        m_Log.Error(message);
                        break;
                    case LogLevel.Info:
                        m_Log.Info(message);
                        break;
                    case LogLevel.Warning:
                        m_Log.Warn(message);
                        break;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                readerWriterLockSlim.ExitWriteLock();
            }
        }
    }
}
